using UnityEngine;
using QFramework;
using System.Collections.Generic;
using System;
using System.Collections;

///手枪
namespace QFramework.Example
{
	public partial class Pistol : Gun
    {
        //public override BulletFathers bulletObject => PistolBullet;

        public override AudioSource AudioSource => SelfAudioSource;

        public override float AtkTime => atkTime;

        private float atkTime = 0;

        private Vector2 dics;
        ShootDuration shootDuration;
        bool isDoubleWapen = false;

        private void Start()
        {
            DataManager.Instance.pistolModel.IsUlockedDouble.Register(value =>
            {

                isDoubleWapen = value;
                GunTwo.Show();

            }).UnRegisterWhenGameObjectDestroyed(gameObject);



              shootDuration = 
                new ShootDuration().WithDuration(DataManager.Instance.
                pistolModel.PistolAtkSpeed.Value*DataManager.Instance.abilityUpModel.BulletSpeeds.Value);

            SetInputAction(() => Shooting(dics),
                () => ShootDown(dics),
                () => ShootUp(dics));

             RemoveBulletCount();

        }

        private void Update()
        {
            MouseDown();
        }

        public override void Shoot(Vector2 dic)
        {
            base.Shoot(dic);

            DataManager.Instance.pistolModel.
                         CanFireIndex.Value -= 1;
            var playerBullet = PoolManager.Instance.GetObj("Bullets/PistolBullet"); //Instantiate(PistolBullet); 
            playerBullet.transform.position = PistolBullet.transform.position;

            if (isDoubleWapen)
            {
                var playerBulletTwo = PoolManager.Instance.GetObj("Bullets/PistolBullet");
                playerBulletTwo.transform.position = PistolBulletTwo.transform.position;


                playerBulletTwo.GetComponent<PistolBullet>().BulletMoveDic(-dic);
                playerBulletTwo.GetComponent<PistolBullet>().SetBulletScale(DataManager.
                     Instance.pistolModel.PistoBulletScale.Value *
               DataManager.Instance.abilityUpModel.BulletScale.Value);



                playerBulletTwo.GetComponent<PistolBullet>().SetBulletSpeed(DataManager.
            Instance.pistolModel.PistolBulletSpeed.Value);


                playerBulletTwo.Show();
            }
           

            //playerBullet.dics = dic;
            playerBullet.GetComponent<PistolBullet>().BulletMoveDic(dic);
            playerBullet.GetComponent<PistolBullet>().SetBulletScale(DataManager.
                Instance.pistolModel.PistoBulletScale.Value*
                DataManager.Instance.abilityUpModel.BulletScale.Value);
            playerBullet.GetComponent<PistolBullet>().SetBulletSpeed(DataManager.
                Instance.pistolModel.PistolBulletSpeed.Value);

          
              
          
            playerBullet.Show();
            AudioSource.clip = ShootSounds[0];
            AudioSource.Play();
        }

        public override void ShootDown(Vector2 dic)
        {
            base.ShootDown(dic);
           
            if (shootDuration.CanShoot&&
                DataManager.Instance.pistolModel.
                CanFireIndex.Value>0)
            {
                shootDuration.RecoreShootTime();
                Shoot(dic);
            }

          
        }

        public override void Shooting(Vector2 dic)
        {
            base.Shooting(dic);
           
            ShootDown(dic);
        }

        public override void ShootUp(Vector2 dic)
        {
            base.ShootUp(dic);
        }


        public override void GetDic(Vector2 dic)
        {
            base.GetDic(dic);
            dics = dic;
        }

        public override void RemoveBulletCount()
        {
            base.RemoveBulletCount();

            DataManager.
                Instance.
                pistolModel.
                CanFireIndex.
                Register(value =>
            {
                if (value <= 0)
                {
                    ///换弹
                    this.SendCommand<RemoveBulletCommands>();
                    this.Delay(0.25f, () => StartCoroutine(RemoveBullet(DataManager.Instance.pistolModel.PistolRemoveSpeed.Value)));
               
                  
                }

            }).UnRegisterWhenGameObjectDestroyed(gameObject);
            
        }


        protected override IEnumerator RemoveBullet(float time)
        {
            return base.RemoveBullet(time);
        }

        public override void OnReloadStart()
        {
            base.OnReloadStart();
            Guns.Hide();
          
            RemoveAnim.Show();
          

            if (isDoubleWapen)
            {
                GunTwo.Hide();
                RemoveAnimTwo.Show();
            }

           
        }

        public override void OnReloadFinished()
        {
            base.OnReloadFinished();
            RemoveAnim.Hide();
           
            DataManager.Instance.pistolModel.CanFireIndex.Value = 
                DataManager.Instance.pistolModel.PistolBulletIndex.Value;
            Guns.Show();
          

            if (isDoubleWapen)
            {
                RemoveAnimTwo.Hide();
                GunTwo.Show();
            }
        }




    }
}
